<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../Universal.css">
</head>
<body>
    
    <div id="cnblogs_post_body" class="blogpost-body">
        <h1>Scanner<span style="font-family: 华文楷体">类</span></h1>
    <p>Scanner<span style="font-family: 宋体">类</span>属于引用数据类型，先了解下引用数据类型。</p>
    <h3 class="18">引用数据类型的使用</h3>
    <p>与定义基本数据类型变量不同，引用数据类型的变量定义及赋值有一个相对固定的步骤或格式。</p>
    <div class="cnblogs_code">
    <pre>数据类型  变量名  =  <span style="color: rgba(0, 0, 255, 1)">new</span> 数据类型();</pre>
    </div>
    <p>每种引用数据类型都有其功能，我们可以调用该类型实例使用其功能。</p>
    <div class="cnblogs_code">
    <pre>变量名.方法名();</pre>
    </div>
    <h2><span style="line-height: 1.5">Scanner类</span></h2>
    <p>Scanner<span style="font-family: 宋体">类可以完成用户键盘录入，获取到录入的数据。</span></p>
    <p>Scanner<span style="font-family: 宋体">使用步骤：</span></p>
    <p><span style="font-family: 宋体">导包：&nbsp;<span class="cnblogs_code">import java.util.Scanner;</span>&nbsp;</span></p>
    <p><span style="font-family: 宋体">创建对象实例：<span class="cnblogs_code">Scanner sc = <span style="color: rgba(0, 0, 255, 1)">new</span> Scanner(System.<span style="color: rgba(0, 0, 255, 1)">in</span>);</span>&nbsp;</span></p>
    <p>调用方法：</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">int</span>  i =<span style="color: rgba(0, 0, 0, 1)"> sc.nextInt(); 用来接收控制台录入的数字
    
    String s </span>= sc.next(); 用来接收控制台录入的字符串</pre>
    </div>
    <p><span style="font-family: 宋体">了解完</span><span style="line-height: 1.5">Scanner</span><span style="font-family: 宋体">类，我们编写代码来使用下它：</span><span style="font-family: Calibri">ScannerDemo01.java</span></p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 0, 1)">import java.util.Scanner;
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ScannerDemo01 {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建Scanner引用类型的变量</span>
            Scanner sc = <span style="color: rgba(0, 0, 255, 1)">new</span> Scanner(System.<span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)">);
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取数字</span>
            System.<span style="color: rgba(0, 0, 255, 1)">out</span>.println(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">请输入一个数字</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> n =<span style="color: rgba(0, 0, 0, 1)"> sc.nextInt();
            System.</span><span style="color: rgba(0, 0, 255, 1)">out</span>.println(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">n的值为</span><span style="color: rgba(128, 0, 0, 1)">"</span> +<span style="color: rgba(0, 0, 0, 1)"> n);
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取字符串</span>
            String str =<span style="color: rgba(0, 0, 0, 1)"> sc.next();
            System.</span><span style="color: rgba(0, 0, 255, 1)">out</span>.println(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">str的值为</span><span style="color: rgba(128, 0, 0, 1)">"</span> +<span style="color: rgba(0, 0, 0, 1)"> str);
    
        }
    }</span></pre>
    </div>
    <p>运行结果如下图所示:</p>
    <p class="23">&nbsp;<img src="https://images2015.cnblogs.com/blog/612774/201707/612774-20170702115309524-1586429438.png" alt=""></p>
    <h2><strong><span style="font-size: 1.5em; line-height: 1.5">&nbsp;</span><span style="font-family: 华文楷体">随机数类</span>Random</strong></h2>
    <p><span style="font-family: 宋体">用来产生随机数的类</span>Random<span style="font-family: 宋体">，它也属于引用数据类型。</span></p>
    <p><span style="font-family: 宋体">这个</span>Random<span style="font-family: 宋体">类，它可以产生多种数据类型的随机数，在这里主要介绍生成整数与小数的方式。</span></p>
    <p class="18">方法简介</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span> nextInt(<span style="color: rgba(0, 0, 255, 1)">int</span> maxValue) 产生[<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">,maxValue)范围的随机整数，包含0，不包含maxValue；
    
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">double</span> nextDouble()  产生[<span style="color: rgba(128, 0, 128, 1)">0</span>,<span style="color: rgba(128, 0, 128, 1)">1</span>)范围的随机小数，包含0.<span style="color: rgba(128, 0, 128, 1)">0</span>，不包含1.<span style="color: rgba(128, 0, 128, 1)">0</span>。</pre>
    </div>
    <p><span style="line-height: 1.5">&nbsp;Random使用方式:</span></p>
    <p class="18">import导包：&nbsp;<span class="cnblogs_code">java.util.Random </span>&nbsp;</p>
    <p class="18"><span style="font-family: 宋体">创建实例格式&nbsp;<span class="cnblogs_code">：Random 变量名 = <span style="color: rgba(0, 0, 255, 1)">new</span> Random();</span>&nbsp;</span></p>
    <p><span style="font-family: 宋体">接下来，通过一段代码，学习下</span>Random<span style="font-family: 宋体">类的使用，</span><span style="font-family: Calibri">RandomDemo.java</span></p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 0, 1)">import java.util.Random;
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> RandomDemo {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建Random类的实例</span>
            Random r = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Random();
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 得到0-100范围内的随机整数，将产生的随机整数赋值给i变量</span>
            <span style="color: rgba(0, 0, 255, 1)">int</span> i = r.nextInt(<span style="color: rgba(128, 0, 128, 1)">100</span><span style="color: rgba(0, 0, 0, 1)">);
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">得到0.0-1.0范围内的随机小数，将产生的随机小数赋值给d变量</span>
            <span style="color: rgba(0, 0, 255, 1)">double</span> d =<span style="color: rgba(0, 0, 0, 1)"> r.nextDouble();
            System.</span><span style="color: rgba(0, 0, 255, 1)">out</span><span style="color: rgba(0, 0, 0, 1)">.println(i);
            System.</span><span style="color: rgba(0, 0, 255, 1)">out</span><span style="color: rgba(0, 0, 0, 1)">.println(d);
        }
    }</span></pre>
    </div>
    <p>运行结果如下图所示：</p>
    <p class="23">&nbsp;<img src="https://images2015.cnblogs.com/blog/612774/201707/612774-20170702120054883-1319785629.png" alt=""></p>
    <h1>流程控制语句</h1>
    <h2><span style="font-family: 华文楷体">选择结构</span>if</h2>
    <p><span style="font-size: 1.17em; line-height: 1.5">&nbsp;</span><strong style="font-size: 1.17em; line-height: 1.5">if<span style="font-family: 黑体">语句</span></strong></p>
    <p>if<span style="font-family: 宋体">语句是指如果满足某种条件，就进行某种处理。</span></p>
    <p><span><span style="font-family: 宋体">在</span><span style="font-family: Calibri">Java</span><span style="font-family: 宋体">中，</span><span style="font-family: Calibri">if</span><span style="font-family: 宋体">语句的具体语法格式如下：</span></span></p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (条件语句){
    
    执行语句;
    
    ……
    
    }</span></pre>
    </div>
    <p><span style="font-family: 宋体">上述格式中，判断条件是一个布尔值，当判断条件为</span>true<span style="font-family: 宋体">时，</span><span style="font-family: Calibri">{}</span><span style="font-family: 宋体">中的执行语句才会执行。</span></p>
    <p class="23"><span style="font-family: 宋体">接下来通过一段代码，学习一下</span>if<span style="font-family: 宋体">语句的具体用法，</span>IfDemo01.java&nbsp;</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">//定义了一个变量x，其初始值为5。在if语句的判断条件中判断x的值是否小于10，很明显条件成立，{}中的语句会被执行，变量x的值将进行自增。从运行结果可以看出，x的值已由原来的5变成了6。<br>public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> IfDemo01 {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> x = 5<span style="color: rgba(0, 0, 0, 1)">;
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (x &lt; 10<span style="color: rgba(0, 0, 0, 1)">) {
                x</span>++<span style="color: rgba(0, 0, 0, 1)">;
            }
            System.out.println(</span>"x=" +<span style="color: rgba(0, 0, 0, 1)"> x);//x=6
        }
    }</span></pre>
    </div>
    <h3><strong style="font-size: 1.17em; line-height: 1.5">if…</strong><strong style="font-size: 1.17em; line-height: 1.5">else<span style="font-family: 黑体">语句</span></strong></h3>
    <p>if…else<span style="font-family: 宋体">语句是指如果满足某种条件，就进行某种处理，否则就进行另一种处理。</span>if…else<span style="font-family: 宋体">语句具体语法格式如下：</span></p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (判断条件){
        执行语句1
        ……
    }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{
        执行语句2
        ……
    }</span></pre>
    </div>
    <p><span style="font-family: 宋体">上述格式中，判断条件是一个布尔值。当判断条件为</span><span style="line-height: 1.5">true</span><span style="font-family: 宋体">时，</span><span style="font-family: Calibri">if</span><span style="font-family: 宋体">后面</span><span style="font-family: Calibri">{}</span><span style="font-family: 宋体">中的执行语句</span><span style="font-family: Calibri">1</span><span style="font-family: 宋体">会执行。当判断条件为</span><span style="font-family: Calibri">false</span><span style="font-family: 宋体">时，</span><span style="font-family: Calibri">else</span><span style="font-family: 宋体">后面</span><span style="font-family: Calibri">{}</span><span style="font-family: 宋体">中的执行语句</span><span style="font-family: Calibri">2</span><span style="font-family: 宋体">会执行。</span></p>
    <p><span style="font-family: 宋体">接下来通过一段代码，来实现判断奇偶数的程序，</span><span style="line-height: 1.5">IfDemo02.java</span></p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> IfDemo02 {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> num = 19<span style="color: rgba(0, 0, 0, 1)">;
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (num % 2 == 0<span style="color: rgba(0, 0, 0, 1)">) {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 判断条件成立，num被2整除</span>
                System.out.println("num是一个偶数"<span style="color: rgba(0, 0, 0, 1)">);
            } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
                System.out.println(</span>"num是一个奇数"<span style="color: rgba(0, 0, 0, 1)">);
            }
        }
    }
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">num是一个奇数</span></pre>
    </div>
    <p><span style="font-family: 宋体">上述代码中，变量</span>num<span style="font-family: 宋体">的值为</span><span style="font-family: Calibri">19</span><span style="font-family: 宋体">，模以</span><span style="font-family: Calibri">2</span><span style="font-family: 宋体">的结果为</span><span style="font-family: Calibri">1</span><span style="font-family: 宋体">，不等于</span><span style="font-family: Calibri">0</span><span style="font-family: 宋体">，判断条件不成立。因此会执行</span><span style="font-family: Calibri">else</span><span style="font-family: 宋体">后面</span><span style="font-family: Calibri">{}</span><span style="font-family: 宋体">中的语句，打印“</span><span style="font-family: Calibri">num</span><span style="font-family: 宋体">是一个奇数”。</span></p>
    <h3><strong>if…</strong><strong>else if</strong><strong>…</strong><strong>else<span style="font-family: 黑体">语句</span></strong></h3>
    <p>if…else if…else<span style="font-family: 宋体">语句用于对多个条件进行判断，进行多种不同的处理。</span><span style="font-family: Calibri">if</span>…else if…else<span style="font-family: 宋体">语句具体语法格式如下：</span></p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (判断条件1) {
    　　执行语句1
    } </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (判断条件2) {
    　　执行语句2
    }
    ...
    </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (判断条件n) {
    　　执行语句n
    } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
    　　执行语句n</span>+1<span style="color: rgba(0, 0, 0, 1)">
    }</span></pre>
    </div>
    <p><span style="font-family: 宋体">上述格式中，判断条件是一个布尔值。当判断条件</span>1<span style="font-family: 宋体">为</span><span style="font-family: Calibri">true</span><span style="font-family: 宋体">时，</span><span style="font-family: Calibri">if</span><span style="font-family: 宋体">后面</span><span style="font-family: Calibri">{}</span><span style="font-family: 宋体">中的执行语句</span><span style="font-family: Calibri">1</span><span style="font-family: 宋体">会执行。当判断条件</span><span style="font-family: Calibri">1</span><span style="font-family: 宋体">为</span><span style="font-family: Calibri">false</span><span style="font-family: 宋体">时，会继续执行判断条件</span><span style="font-family: Calibri">2</span><span style="font-family: 宋体">，如果为</span><span style="font-family: Calibri">true</span><span style="font-family: 宋体">则执行语句</span><span style="font-family: Calibri">2</span><span style="font-family: 宋体">，以此类推，如果所有的判断条件都为</span><span style="font-family: Calibri">false</span><span style="font-family: 宋体">，则意味着所有条件均未满足，</span><span style="font-family: Calibri">else</span><span style="font-family: 宋体">后面</span><span style="font-family: Calibri">{}</span><span style="font-family: 宋体">中的执行语句</span><span style="font-family: Calibri">n+1</span><span style="font-family: 宋体">会执行。</span></p>
    <p class="24" align="justify"><span style="font-family: 宋体">接下来通过一段代码，来实现对学生考试成绩进行等级划分的程序，</span>IfDemo03.java</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> IfDemo03 {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> grade = 75; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 定义学生成绩</span>
            <span style="color: rgba(0, 0, 255, 1)">if</span> (grade &gt; 80<span style="color: rgba(0, 0, 0, 1)">) {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 满足条件 grade &gt; 80</span>
                System.out.println("该成绩的等级为优"<span style="color: rgba(0, 0, 0, 1)">);
            } </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (grade &gt; 70<span style="color: rgba(0, 0, 0, 1)">) {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 不满足条件 grade &gt; 80 ，但满足条件 grade &gt; 70</span>
                System.out.println("该成绩的等级为良"<span style="color: rgba(0, 0, 0, 1)">);
            } </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (grade &gt; 60<span style="color: rgba(0, 0, 0, 1)">) {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 不满足条件 grade &gt; 70 ，但满足条件 grade &gt; 60</span>
                System.out.println("该成绩的等级为中"<span style="color: rgba(0, 0, 0, 1)">);
            } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 不满足条件 grade &gt; 60</span>
                System.out.println("该成绩的等级为差"<span style="color: rgba(0, 0, 0, 1)">);
            }
        }
    }
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">该成绩的等级为良</span></pre>
    </div>
    <p><span style="font-family: 宋体">上述代码中，定义了学生成绩</span><span style="line-height: 1.5">grade</span><span style="font-family: 宋体">为</span><span style="font-family: Calibri">75</span><span style="font-family: 宋体">。它不满足第一个判断条件</span><span style="font-family: Calibri">grade&gt;80</span><span style="font-family: 宋体">，会执行第二个判断条件</span><span style="font-family: Calibri">grade&gt;70</span><span style="font-family: 宋体">，条件成立，因此会打印“</span><span style="font-family: 宋体">该成绩的等级为良</span><span style="line-height: 1.5">”。</span></p>
    <h3><strong><span style="font-family: 黑体">选择结构</span>if<span style="font-family: 黑体">语句与三元运算转换</span></strong></h3>
    <p>三元运算符，它和if-else语句类似，语法如下：</p>
    <div class="cnblogs_code">
    <pre>判断条件 ? 表达式1 : 表达式2</pre>
    </div>
    <p>三元运算符会得到一个结果，通常用于对某个变量进行赋值，当判断条件成立时，运算结果为表达式1的值，否则结果为表达式2的值。</p>
    <p>例如求两个数x<span style="font-family: 宋体">、</span><span style="font-family: &quot;Times New Roman&quot;">y</span>中的较大者，如果用if…else语句来实现，具体代码如下：</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">int</span> x = 0<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">int</span> y = 1<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> max;
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (x &gt;<span style="color: rgba(0, 0, 0, 1)"> y) {
    　　max </span>=<span style="color: rgba(0, 0, 0, 1)"> x;
    } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
    　　max </span>=<span style="color: rgba(0, 0, 0, 1)"> y;
    }<br></span></pre>
    </div>
    <p>可以替换为</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">int</span> x=0<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">int</span> y=1<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">int</span> max = x &gt; y ? x : y;</pre>
    </div>
    <h2><strong><span style="font-family: 华文楷体">switch语句</span></strong></h2>
    <p><span style="font-family: 华文楷体"><span class="fontstyle0">根据一个整数表达式的值， switch 语句可从一系列</span></span><span style="font-family: 华文楷体"><span class="fontstyle0">代码选出一段执行。它的格式如下：</span></span></p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">switch</span><span style="color: rgba(0, 0, 0, 1)">(整数选择因子或者字符串或者枚举) {
    　　</span><span style="color: rgba(0, 0, 255, 1)">case</span> 整数值 1 : 语句; <span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
    　　</span><span style="color: rgba(0, 0, 255, 1)">case</span> 整数值 2 : 语句; <span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
    　　</span><span style="color: rgba(0, 0, 255, 1)">case</span> 整数值 3 : 语句; <span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
    　　</span><span style="color: rgba(0, 0, 255, 1)">case</span> 整数值 4 : 语句; <span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
    　　</span><span style="color: rgba(0, 0, 255, 1)">case</span> 整数值 5 : 语句; <span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
    　　</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">..</span>
    　　<span style="color: rgba(0, 0, 255, 1)">default</span>:语句;92<span style="color: rgba(0, 0, 0, 1)">
    }</span></pre>
    </div>
    <p><span class="fontstyle0">switch 能将整数选择因子的结果与每个整数值比较。若发现相符的，就执行对应的语句（简单或复合语句）。若没有发现相符的，就执行default 语句。 示例：</span>VowelsAndConsonants.java</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> VowelsAndConsonants {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
            </span><span style="color: rgba(0, 0, 255, 1)">char</span> c = (<span style="color: rgba(0, 0, 255, 1)">char</span>) (Math.random() * 26 + 'a'<span style="color: rgba(0, 0, 0, 1)">);
            System.out.print(c </span>+ ": "<span style="color: rgba(0, 0, 0, 1)">);
            </span><span style="color: rgba(0, 0, 255, 1)">switch</span><span style="color: rgba(0, 0, 0, 1)"> (c) {
                </span><span style="color: rgba(0, 0, 255, 1)">case</span> 'a'<span style="color: rgba(0, 0, 0, 1)">:
                </span><span style="color: rgba(0, 0, 255, 1)">case</span> 'e'<span style="color: rgba(0, 0, 0, 1)">:
                </span><span style="color: rgba(0, 0, 255, 1)">case</span> 'i'<span style="color: rgba(0, 0, 0, 1)">:
                </span><span style="color: rgba(0, 0, 255, 1)">case</span> 'o'<span style="color: rgba(0, 0, 0, 1)">:
                </span><span style="color: rgba(0, 0, 255, 1)">case</span> 'u'<span style="color: rgba(0, 0, 0, 1)">:
                    System.out.println(</span>"vowel"<span style="color: rgba(0, 0, 0, 1)">);
                    </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
                </span><span style="color: rgba(0, 0, 255, 1)">case</span> 'y'<span style="color: rgba(0, 0, 0, 1)">:
                </span><span style="color: rgba(0, 0, 255, 1)">case</span> 'w'<span style="color: rgba(0, 0, 0, 1)">:
                    System.out.println(
                            </span>"Sometimes a vowel"<span style="color: rgba(0, 0, 0, 1)">);
                    </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
                </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)">:
                    System.out.println(</span>"consonant"<span style="color: rgba(0, 0, 0, 1)">);
            }
        }
    }</span></pre>
    </div>
    <h3>switch语句接受的数据类型</h3>
    <p>switch语句中的表达式的数据类型,是有要求的</p>
    <ul>
    <li>JDK1.0 - 1.4 数据类型接受 byte short int char</li>
    <li>JDK1.5 数据类型接受 byte short int char enum(枚举)</li>
    <li>JDK1.7 数据类型接受 byte short int char enum(枚举), String</li>
    </ul>
    <h3>case穿透</h3>
    <p>在使用switch语句的过程中，如果多个case条件后面的执行语句是一样的，则该执行语句只需书写一次即可，这是一种简写的方式。<br>例如，要判断一周中的某一天是否为工作日，同样使用数字1~7来表示星期一到星期天，当输入的数字为1、2、3、4、5时就视为工作日，否则就视为休息日。如下所示。SwitchDemo02.java</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> SwitchDemo02 {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> week = 2<span style="color: rgba(0, 0, 0, 1)">;
            </span><span style="color: rgba(0, 0, 255, 1)">switch</span><span style="color: rgba(0, 0, 0, 1)"> (week) {
                </span><span style="color: rgba(0, 0, 255, 1)">case</span> 1<span style="color: rgba(0, 0, 0, 1)">:
                </span><span style="color: rgba(0, 0, 255, 1)">case</span> 2<span style="color: rgba(0, 0, 0, 1)">:
                </span><span style="color: rgba(0, 0, 255, 1)">case</span> 3<span style="color: rgba(0, 0, 0, 1)">:
                </span><span style="color: rgba(0, 0, 255, 1)">case</span> 4<span style="color: rgba(0, 0, 0, 1)">:
                </span><span style="color: rgba(0, 0, 255, 1)">case</span> 5<span style="color: rgba(0, 0, 0, 1)">:
                    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 当 week 满足值 1、2、3、4、5 中任意一个时，处理方式相同</span>
                    System.out.println("今天是工作日"<span style="color: rgba(0, 0, 0, 1)">);
                    </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
                </span><span style="color: rgba(0, 0, 255, 1)">case</span> 6<span style="color: rgba(0, 0, 0, 1)">:
                </span><span style="color: rgba(0, 0, 255, 1)">case</span> 7<span style="color: rgba(0, 0, 0, 1)">:
                    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 当 week 满足值 6、7 中任意一个时，处理方式相同</span>
                    System.out.println("今天是休息日"<span style="color: rgba(0, 0, 0, 1)">);
                    </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
            }
        }
    }</span></pre>
    </div>
    <p><span style="font-family: 华文楷体; font-size: 1.5em; line-height: 1.5"><img src="https://images2015.cnblogs.com/blog/612774/201707/612774-20170702135211633-941232882.png" alt=""></span></p>
    <p><span style="font-family: 宋体">上述代码中，当变量</span>week<span style="font-family: 宋体">值为</span><span style="font-family: Calibri">1</span><span style="font-family: 宋体">、</span><span style="font-family: Calibri">2</span><span style="font-family: 宋体">、</span><span style="font-family: Calibri">3</span><span style="font-family: 宋体">、</span><span style="font-family: Calibri">4</span><span style="font-family: 宋体">、</span><span style="font-family: Calibri">5</span><span style="font-family: 宋体">中任意一个值时，处理方式相同，都会打印“今天是工作日”。同理，当变量</span><span style="font-family: Calibri">week</span><span style="font-family: 宋体">值为</span><span style="font-family: Calibri">6</span><span style="font-family: 宋体">、</span><span style="font-family: Calibri">7</span><span style="font-family: 宋体">中任意一个值时，打印“今天是休息日”。</span></p>
    <h1><span style="font-family: 华文楷体; font-size: 1.5em; line-height: 1.5">循环语句</span></h1>
    <h2>while语句</h2>
    <p>while<span style="font-family: 宋体">循环语句和选择结构</span><span style="font-family: Calibri">if</span><span style="font-family: 宋体">语句有些相似，都是根据条件判断来决定是否执行大括号内的执行语句。区别在于，</span><span style="font-family: Calibri">while</span><span style="font-family: 宋体">语句会反复地进行条件判断，只要条件成立，</span><span style="font-family: Calibri">{}</span><span style="font-family: 宋体">内的执行语句就会执行，直到条件不成立，</span><span style="font-family: Calibri">while</span><span style="font-family: 宋体">循环结束。</span>while循环语句的语法结构如下：</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)">(循环条件){
        执行语句
        ………
    }</span></pre>
    </div>
    <p><span style="font-family: 宋体">在上面的语法结构中，</span><span style="line-height: 1.5">{}</span><span style="font-family: 宋体">中的执行语句被称作循环体，循环体是否执行取决于循环条件。当循环条件为</span><span style="font-family: Calibri">true</span><span style="font-family: 宋体">时，循环体就会执行。循环体执行完毕时会继续判断循环条件，如条件仍为</span><span style="font-family: Calibri">true</span><span style="font-family: 宋体">则会继续执行，直到循环条件为</span><span style="font-family: Calibri">false</span><span style="font-family: 宋体">时，整个循环过程才会结束。</span></p>
    <p><span style="font-family: 宋体">接下来通过一段代码，来实现打印</span><span style="line-height: 1.5">1~4</span><span style="font-family: 宋体">之间的自然数，</span><span style="line-height: 1.5">WhileDemo.java</span></p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> WhileDemo {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> x = 1; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 定义变量x，初始值为1</span>
            <span style="color: rgba(0, 0, 255, 1)">while</span> (x &lt;= 4) { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 循环条件</span>
                System.out.println("x = " + x); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 条件成立，打印x的值</span>
                x++; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> x进行自增</span>
    <span style="color: rgba(0, 0, 0, 1)">        }
        }
    }
    
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">x = 1
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">x = 2
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">x = 3
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">x = 4</span></pre>
    </div>
    <p><span style="font-family: 宋体">在上述代码中，</span><span style="line-height: 1.5">x</span><span style="font-family: 宋体">初始值为</span><span style="font-family: Calibri">1</span><span style="font-family: 宋体">，在满足循环条件</span><span style="line-height: 1.5">x &lt;=&nbsp;4</span><span style="font-family: 宋体">的情况下，循环体会重复执行，打印</span><span style="font-family: Calibri">x</span><span style="font-family: 宋体">的值并让</span><span style="font-family: Calibri">x</span><span style="font-family: 宋体">进行自增。因此打印结果中</span><span style="font-family: Calibri">x</span><span style="font-family: 宋体">的值分别为</span><span style="font-family: Calibri">1</span><span style="font-family: 宋体">、</span><span style="font-family: Calibri">2</span><span style="font-family: 宋体">、</span><span style="font-family: Calibri">3</span><span style="font-family: 宋体">、</span><span style="font-family: Calibri">4</span><span style="font-family: 宋体">。</span></p>
    <p><span style="font-family: 宋体">值得注意的是，代码</span>x++<span style="font-family: 宋体">用于在每次循环时改变变量</span><span style="font-family: Calibri">x</span><span style="font-family: 宋体">的值，从而达到最终改变循环条件的目的。如果没有这行代码，整个循环会进入无限循环的状态，永远不会结束。</span></p>
    <h2><strong><span style="font-family: 华文楷体">循环语句</span>for</strong></h2>
    <p>for<span style="font-family: 宋体">循环语句是最常用的循环语句，一般用在循环次数已知的情况下。</span><span style="font-family: Calibri">for</span><span style="font-family: 宋体">循环语句的语法格式如下：</span></p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)">（初始化表达式; 循环条件; 操作表达式）{
    
        执行语句
        ………
    }</span></pre>
    </div>
    <p><span style="font-family: 宋体">在上面的语法结构中，</span><span style="line-height: 1.5">for</span><span style="font-family: 宋体">关键字后面</span><span style="font-family: Calibri">()</span><span style="font-family: 宋体">中包括了三部分内容：初始化表达式、循环条件和操作表达式，它们之间用“</span><span style="font-family: Calibri">;</span><span style="font-family: 宋体">”分隔，</span><span style="font-family: Calibri">{}</span><span style="font-family: 宋体">中的执行语句为循环体。</span></p>
    <p><span style="font-family: 宋体">接下来分别用</span>①表示初始化表达式、②表示循环条件、③表示操作表达式、④表示循环体，通过序号来具体分析<span style="font-family: Calibri">for</span><span style="font-family: 宋体">循环的执行流程。具体如下：</span></p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)">（① ; ② ; ③）{
    　　④
    }</span></pre>
    </div>
    <ul>
    <li><span style="font-family: 宋体">第一步，执行</span>①</li>
    <li class="17"><span style="font-family: 宋体">第二步，执行</span>②，如果判断结果为<span style="font-family: &quot;Courier New&quot;">true</span><span style="font-family: 宋体">，执行第三步，如果判断结果为</span><span style="font-family: &quot;Courier New&quot;">false</span><span style="font-family: 宋体">，执行第五步</span></li>
    <li class="17"><span style="font-family: 宋体">第三步，执行</span>④</li>
    <li class="17"><span style="font-family: 宋体">第四步，执行</span>③，然后重复执行第二步</li>
    <li class="17"><span style="font-family: 宋体">第五步，退出循环</span></li>
    </ul>
    <p class="24" align="justify"><span style="font-family: 宋体">接下来通过一个案例对自然数</span>1~4<span style="font-family: 宋体">进行求和，如下所示。</span>ForDemo01.java</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ForDemo01 {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> sum = 0; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 定义变量sum，用于记住累加的和</span>
            <span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 1; i &lt;= 4; i++) { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> i的值会在1~4之间变化</span>
                sum += i; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 实现sum与i的累加</span>
    <span style="color: rgba(0, 0, 0, 1)">        }
            System.out.println(</span>"sum = " + sum); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 打印累加的和</span>
    <span style="color: rgba(0, 0, 0, 1)">    }
    }</span></pre>
    </div>
    <p><span style="font-family: 宋体">上述代码中，变量</span><span style="line-height: 1.5">i</span><span style="font-family: 宋体">的初始值为</span><span style="font-family: Calibri">1</span><span style="font-family: 宋体">，在判断条件</span><span style="font-family: Calibri">i&lt;=4</span><span style="font-family: 宋体">为</span><span style="font-family: Calibri">true</span><span style="font-family: 宋体">的情况下，会执行循环体</span><span style="font-family: Calibri">sum+=i</span><span style="font-family: 宋体">，执行完毕后，会执行操作表达式</span><span style="font-family: Calibri">i++</span><span style="font-family: 宋体">，</span><span style="font-family: Calibri">i</span><span style="font-family: 宋体">的值变为</span><span style="font-family: Calibri">2</span><span style="font-family: 宋体">，然后继续进行条件判断，开始下一次循环，直到</span><span style="font-family: Calibri">i=5</span><span style="font-family: 宋体">时，条件</span><span style="font-family: Calibri">i&lt;=4</span><span style="font-family: 宋体">为</span><span style="font-family: Calibri">false</span><span style="font-family: 宋体">，结束循环，执行</span><span style="font-family: Calibri">for</span><span style="font-family: 宋体">循环后面的代码，打印“</span><span style="font-family: Calibri">sum=10</span><span style="font-family: 宋体">”。</span></p>
    <h2>do…while语句</h2>
    <p>do…while<span style="font-family: 宋体">循环语句和</span><span style="font-family: Calibri">while</span><span style="font-family: 宋体">循环语句功能类似，其语法结构如下：</span></p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">do</span><span style="color: rgba(0, 0, 0, 1)"> {
    
        执行语句
        ………
    } </span><span style="color: rgba(0, 0, 255, 1)">while</span>(循环条件);</pre>
    </div>
    <p><span style="font-family: 宋体">在上面的语法结构中，关键字</span><span style="line-height: 1.5">do</span><span style="font-family: 宋体">后面</span><span style="font-family: Calibri">{}</span><span style="font-family: 宋体">中的执行语句是循环体。</span><span style="font-family: Calibri">do</span><span style="line-height: 1.5">…while</span><span style="font-family: 宋体">循环语句将循环条件放在了循环体的后面。这也就意味着，循环体会无条件执行一次，然后再根据循环条件来决定是否继续执行。</span></p>
    <p><span style="font-family: 宋体">接下来使用</span><span style="line-height: 1.5">do…while</span><span style="font-family: 宋体">循环语句来实现打印</span><span style="font-family: Calibri">1~4</span><span style="font-family: 宋体">之间的自然数</span><span style="font-family: Calibri">DoWhileDemo.java</span><span style="font-family: 宋体">。</span></p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> DoWhileDemo {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> x = 1; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 定义变量x，初始值为1</span>
            <span style="color: rgba(0, 0, 255, 1)">do</span><span style="color: rgba(0, 0, 0, 1)"> {
                System.out.println(</span>"x = " + x); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 打印x的值</span>
                x++; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将x的值自增</span>
            } <span style="color: rgba(0, 0, 255, 1)">while</span> (x &lt;= 4); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 循环条件</span>
    <span style="color: rgba(0, 0, 0, 1)">    }
    }</span></pre>
    </div>
    <p><span style="font-family: Calibri">do </span><span style="line-height: 1.5">…while</span><span style="font-family: 宋体">循环和</span><span style="font-family: Calibri">while</span><span style="font-family: 宋体">循环能实现同样的功能。然而在程序运行过程中，这两种语句还是有差别的。如果循环条件在循环语句开始时就不成立，那么</span><span style="font-family: Calibri">while</span><span style="font-family: 宋体">循环的循环体一次都不会执行，而</span><span style="font-family: Calibri">do</span><span style="line-height: 1.5">…while</span><span style="font-family: 宋体">循环的循环体还是会执行一次。若将</span><span style="font-family: Calibri">DoWhileDemo.java</span><span style="font-family: 宋体">中的循环条件</span><span style="font-family: Calibri">x&lt;=4</span><span style="font-family: 宋体">改为</span><span style="font-family: Calibri">x &lt; 1</span><span style="font-family: 宋体">，</span><span style="font-family: Calibri">DoWhileDemo.java</span><span style="font-family: 宋体">运行结果会打印</span><span style="font-family: Calibri">x=1</span><span style="font-family: 宋体">，而</span><span style="font-family: Calibri">WhileDemo.java</span><span style="font-family: 宋体">运行结果什么也不会打印。</span></p>
    <h3><strong>无限循环</strong></h3>
    <p>最简单无限循环格式：</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">while</span>(<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">){}
    
    或
    
    </span><span style="color: rgba(0, 0, 255, 1)">for</span>(;;){}</pre>
    </div>
    <p><span style="line-height: 1.5">无限循环存在的原因是并不知道循环多少次，而是根据某些条件，来控制循环。</span></p>
    <h3><span style="font-family: 华文楷体">循环嵌套</span></h3>
    <p><span style="font-family: 宋体">嵌套循环是指在一个循环语句的循环体中再定义一个循环语句的语法结构。</span>while<span style="font-family: 宋体">、</span><span style="font-family: Calibri">do</span>…while<span style="font-family: 宋体">、</span><span style="font-family: Calibri">for</span><span style="font-family: 宋体">循环语句都可以进行嵌套，并且它们之间也可以互相嵌套，如最常见的在</span><span style="font-family: Calibri">for</span><span style="font-family: 宋体">循环中嵌套</span><span style="font-family: Calibri">for</span><span style="font-family: 宋体">循环，格式如下：</span></p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)">(初始化表达式; 循环条件; 操作表达式) {
        ………
        </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)">(初始化表达式; 循环条件; 操作表达式) {
            执行语句
            ………
         }
        ………
    }</span></pre>
    </div>
    <p><span style="font-family: 宋体">接下来实现使用</span><span style="line-height: 1.5">“</span><span style="font-family: Calibri">*</span><span style="font-family: 宋体">”打印直角三角形，如下所示。</span><span style="font-family: Calibri">ForForDemo.java</span></p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ForForDemo {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> i, j; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 定义两个循环变量</span>
            <span style="color: rgba(0, 0, 255, 1)">for</span> (i = 1; i &lt;= 9; i++) { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 外层循环</span>
                <span style="color: rgba(0, 0, 255, 1)">for</span> (j = 1; j &lt;= i; j++) { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 内层循环</span>
                    System.out.print("*"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 打印*</span>
    <span style="color: rgba(0, 0, 0, 1)">            }
                System.out.print(</span>"\n"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 换行</span>
    <span style="color: rgba(0, 0, 0, 1)">        }
        }
    }</span></pre>
    </div>
    <p>运行结果如下所示：</p>
    <p><img src="https://images2015.cnblogs.com/blog/612774/201707/612774-20170702122412493-715167864.png" alt=""></p>
    <p><span style="font-family: 宋体">在上述代码中定义了两层</span><span style="line-height: 1.5">for</span><span style="font-family: 宋体">循环，分别为外层循环和内层循环，外层循环用于控制打印的行数，内层循环用于打印“</span><span style="font-family: Calibri">*</span><span style="font-family: 宋体">”，每一行的“</span><span style="font-family: Calibri">*</span><span style="font-family: 宋体">”个数逐行增加，最后输出一个直角三角形。由于嵌套循环程序比较复杂，下面分步骤进行详细地讲解，具体如下：</span></p>
    <ul>
    <li><span style="font-family: 宋体">第一步，在第</span>3<span style="font-family: 宋体">行代码定义了两个循环变量</span><span style="font-family: Calibri">i</span><span style="font-family: 宋体">和</span><span style="font-family: Calibri">j</span><span style="font-family: 宋体">，其中</span><span style="font-family: Calibri">i</span><span style="font-family: 宋体">为外层循环变量，</span><span style="font-family: Calibri">j</span><span style="font-family: 宋体">为内层循环变量。</span></li>
    <li><span style="font-family: 宋体">第二步，在第</span>4<span style="font-family: 宋体">行代码将</span><span style="font-family: Calibri">i</span><span style="font-family: 宋体">初始化为</span><span style="font-family: Calibri">1</span><span style="font-family: 宋体">，条件</span>i &lt;= 9<span style="font-family: 宋体">为</span>true<span style="font-family: 宋体">，首次进入外层循环的循环体。</span></li>
    <li><span style="font-family: 宋体">第三步，在第</span>5<span style="font-family: 宋体">行代码将</span><span style="font-family: Calibri">j</span><span style="font-family: 宋体">初始化为</span><span style="font-family: Calibri">1</span><span style="font-family: 宋体">，由于此时</span><span style="font-family: Calibri">i</span><span style="font-family: 宋体">的值为</span><span style="font-family: Calibri">1</span><span style="font-family: 宋体">，条件</span><span style="font-family: Calibri">j</span>&nbsp;&lt;= i<span style="font-family: 宋体">为</span><span style="font-family: Calibri">true</span><span style="font-family: 宋体">，首次进入内层循环的循环体，打印一个“</span><span style="font-family: Calibri">*</span><span style="font-family: 宋体">”。</span></li>
    <li><span style="font-family: 宋体">第四步，执行第</span>5<span style="font-family: 宋体">行代码中内层循环的操作表达式</span><span style="font-family: Calibri">j++</span><span style="font-family: 宋体">，将</span><span style="font-family: Calibri">j</span><span style="font-family: 宋体">的值自增为</span><span style="font-family: Calibri">2</span><span style="font-family: 宋体">。</span></li>
    <li><span style="font-family: 宋体">第五步，执行第</span>5<span style="font-family: 宋体">行代码中的判断条件</span><span style="font-family: Calibri">j&lt;=</span>i<span style="font-family: 宋体">，判断结果为</span>false<span style="font-family: 宋体">，内层循环结束。执行后面的代码，打印换行符。</span></li>
    <li><span style="font-family: 宋体">第六步，执行第</span>4<span style="font-family: 宋体">行代码中外层循环的操作表达式</span><span style="font-family: Calibri">i++</span><span style="font-family: 宋体">，将</span><span style="font-family: Calibri">i</span><span style="font-family: 宋体">的值自增为</span><span style="font-family: Calibri">2</span><span style="font-family: 宋体">。</span></li>
    <li><span style="font-family: 宋体">第七步，执行第</span>4<span style="font-family: 宋体">行代码中的判断条件</span><span style="font-family: Calibri">i&lt;=9</span><span style="font-family: 宋体">，判断结果为</span><span style="font-family: Calibri">true</span><span style="font-family: 宋体">，进入外层循环的循环体，继续执行内层循环。</span></li>
    <li><span style="font-family: 宋体">第八步，由于</span>i<span style="font-family: 宋体">的值为</span><span style="font-family: Calibri">2</span><span style="font-family: 宋体">，内层循环会执行两次，即在第</span><span style="font-family: Calibri">2</span><span style="font-family: 宋体">行打印两个“</span><span style="font-family: Calibri">*</span><span style="font-family: 宋体">”。在内层循环结束时会打印换行符。</span></li>
    <li><span style="font-family: 宋体">第九步，以此类推，在第</span>3<span style="font-family: 宋体">行会打印</span><span style="font-family: Calibri">3</span><span style="font-family: 宋体">个“</span><span style="font-family: Calibri">*</span><span style="font-family: 宋体">”，逐行递增，直到</span><span style="font-family: Calibri">i</span><span style="font-family: 宋体">的值为</span><span style="font-family: Calibri">10</span><span style="font-family: 宋体">时，外层循环的判断条件</span>i &lt;= 9<span style="font-family: 宋体">结果为</span>false<span style="font-family: 宋体">，外层循环结束，整个程序也就结束了。</span></li>
    </ul>
    <h2><span style="font-family: 华文楷体">跳转语句（</span>break<span style="font-family: 华文楷体">、</span><span style="font-family: &quot;Times New Roman&quot;">continue</span><span style="font-family: 华文楷体">）</span></h2>
    <p><span style="font-family: 宋体">跳转语句用于实现循环执行过程中程序流程的跳转，在</span>Java<span style="font-family: 宋体">中的跳转语句有</span><span style="font-family: Calibri">break</span><span style="font-family: 宋体">语句和</span><span style="font-family: Calibri">continue</span><span style="font-family: 宋体">语句。接下来分别进行详细地讲解。</span></p>
    <h3><strong>break<span style="font-family: 宋体">语句</span></strong></h3>
    <p><span style="font-family: 宋体">在</span>switch<span style="font-family: 宋体">条件语句和循环语句中都可以使用</span><span style="font-family: Calibri">break</span><span style="font-family: 宋体">语句。当它出现在</span><span style="font-family: Calibri">switch</span><span style="font-family: 宋体">条件语句中时，作用是终止某个</span><span style="font-family: Calibri">case</span><span style="font-family: 宋体">并跳出</span><span style="font-family: Calibri">switch</span><span style="font-family: 宋体">结构。当它出现在循环语句中，作用是跳出循环语句，执行后面的代码。</span></p>
    <p><span style="font-family: 宋体">接下来通过下面一段代码，实现将当变量</span><span style="font-family: Calibri">x</span><span style="font-family: 宋体">的值为</span><span style="font-family: Calibri">3</span><span style="font-family: 宋体">时，使用</span><span style="font-family: Calibri">break</span><span style="font-family: 宋体">语句跳出循环，代码如下所示。</span><span style="font-family: Calibri">BreakDemo.java</span></p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> BreakDemo {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> x = 1; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 定义变量x，初始值为1</span>
            <span style="color: rgba(0, 0, 255, 1)">while</span> (x &lt;= 4) { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 循环条件</span>
                System.out.println("x = " + x); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 条件成立，打印x的值</span>
                <span style="color: rgba(0, 0, 255, 1)">if</span> (x == 3<span style="color: rgba(0, 0, 0, 1)">) {
                    </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
                }
                x</span>++; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> x进行自增</span>
    <span style="color: rgba(0, 0, 0, 1)">        }
        }
    }
    <br></span></pre>
    </div>
    <pre><span style="font-family: 宋体">在上述带代码中，通过</span><span style="font-family: &quot;PingFang SC&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 1.5">while</span><span style="font-family: 宋体">循环打印</span><span style="font-family: Calibri">x</span><span style="font-family: 宋体">的值，当</span><span style="font-family: Calibri">x</span><span style="font-family: 宋体">的值为</span><span style="font-family: Calibri">3</span><span style="font-family: 宋体">时使用</span><span style="font-family: Calibri">break</span><span style="font-family: 宋体">语句跳出循环。因此打印结果中并没有出现“</span><span style="font-family: Calibri">x=4</span><span style="font-family: 宋体">”。</span></pre>
    <h3><strong>标记</strong></h3>
    <p class="24">当break<span style="font-family: 宋体">语句出现在嵌套循环中的内层循环时，它只能跳出内层循环，如果想使用</span>break<span style="font-family: 宋体">语句跳出外层循环则需要对外层循环添加标记。接下来将</span><span style="font-family: &quot;Times New Roman&quot;">ForForDemo.java</span><span style="font-family: 宋体">稍作修改，控制程序只打印</span><span style="font-family: &quot;Times New Roman&quot;">4</span><span style="font-family: 宋体">行“</span><span style="font-family: &quot;Times New Roman&quot;">*</span><span style="font-family: 宋体">”，如下所示。</span>BreakDemo02.java</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> BreakDemo02 {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> i, j; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 定义两个循环变量</span>
            AA: <span style="color: rgba(0, 0, 255, 1)">for</span> (i = 1; i &lt;= 9; i++) { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 外层循环</span>
                <span style="color: rgba(0, 0, 255, 1)">for</span> (j = 1; j &lt;= i; j++) { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 内层循环</span>
                    <span style="color: rgba(0, 0, 255, 1)">if</span> (i &gt; 4) { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 判断i的值是否大于4</span>
                        <span style="color: rgba(0, 0, 255, 1)">break</span> AA; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 跳出外层循环</span>
    <span style="color: rgba(0, 0, 0, 1)">                }
                    System.out.print(</span>"*"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 打印*</span>
    <span style="color: rgba(0, 0, 0, 1)">            }
                System.out.print(</span>"\n"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 换行</span>
    <span style="color: rgba(0, 0, 0, 1)">        }
        }
    }</span></pre>
    </div>
    <p><img src="https://images2015.cnblogs.com/blog/612774/201707/612774-20170702124101680-38623529.png" alt=""></p>
    <p>BreakDemo02.java<span style="font-family: 宋体">与</span><span style="font-family: Calibri">ForForDemo.java</span><span style="font-family: 宋体">实现原理类似，只是在外层</span><span style="font-family: Calibri">for</span><span style="font-family: 宋体">循环前面增加了标记“</span><span style="font-family: Calibri">AA</span><span style="font-family: 宋体">”。当</span><span style="font-family: Calibri">i&gt;4</span><span style="font-family: 宋体">时，使用</span><span style="font-family: Calibri">break AA;</span><span style="font-family: 宋体">语句跳出外层循环。因此程序只打印了</span><span style="font-family: Calibri">4</span><span style="font-family: 宋体">行“</span><span style="font-family: Calibri">*</span><span style="font-family: 宋体">”。</span></p>
    <h3><strong>continue<span style="font-family: 宋体">语句</span></strong></h3>
    <p>continue<span style="font-family: 宋体">语句用在循环语句中，它的作用是终止本次循环，执行下一次循环。接下来通过一个练习对</span><span style="font-family: Calibri">1~100</span><span style="font-family: 宋体">之内的奇数求和，</span><span style="font-family: Calibri">ContinueDemo.java</span></p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ContinueDemo {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> sum = 0; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 定义变量sum，用于记住和</span>
            <span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 1; i &lt;= 100; i++<span style="color: rgba(0, 0, 0, 1)">) {
                </span><span style="color: rgba(0, 0, 255, 1)">if</span> (i % 2 == 0) { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> i是一个偶数，不累加</span>
                    <span style="color: rgba(0, 0, 255, 1)">continue</span>; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 结束本次循环</span>
    <span style="color: rgba(0, 0, 0, 1)">            }
                sum </span>+= i; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 实现sum和i的累加</span>
    <span style="color: rgba(0, 0, 0, 1)">        }
            System.out.println(</span>"sum = " +<span style="color: rgba(0, 0, 0, 1)"> sum);
        }
    }
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">sum = 2500</span></pre>
    </div>
    <p><span style="font-family: 宋体">上述代码中，使用</span><span style="line-height: 1.5">for</span><span style="font-family: 宋体">循环让变量</span><span style="font-family: Calibri">i</span><span style="font-family: 宋体">的值在</span><span style="font-family: Calibri">1~100</span><span style="font-family: 宋体">之间循环，在循环过程中，当</span><span style="font-family: Calibri">i</span><span style="font-family: 宋体">的值为偶数时，将执行</span><span style="font-family: Calibri">continue</span><span style="font-family: 宋体">语句结束本次循环，进入下一次循环。当</span><span style="font-family: Calibri">i</span><span style="font-family: 宋体">的值为奇数时，</span><span style="font-family: Calibri">sum</span><span style="font-family: 宋体">和</span><span style="font-family: Calibri">i</span><span style="font-family: 宋体">进行累加，最终得到</span><span style="font-family: Calibri">1~100</span><span style="font-family: 宋体">之间所有奇数的和，打印“</span><span style="font-family: Calibri">sum = 2500</span><span style="font-family: 宋体">”。</span></p>
    <p><span style="font-family: 宋体">在嵌套循环语句中，</span>continue<span style="font-family: 宋体">语句后面也可以通过使用标记的方式结束本次外层循环，用法与</span><span style="font-family: Calibri">break</span><span style="font-family: 宋体">语句相似，在此不再举例说明。</span></p>
    <h1>猜数字案例</h1>
    <h3><strong>案例介绍</strong></h3>
    <p><span style="line-height: 1.5">猜数字案例是要完成什么样的功能呢？顾名思义，这个游戏就是你出个数字、我来猜。</span></p>
    <p>游戏操作如下：</p>
    <p class="18"><span style="font-family: 宋体">后台预先生成一个</span>1-100之间的随机数，用户键盘录入猜数字</p>
    <p class="18"><span style="font-family: 宋体">如果猜对了，打印</span>“恭喜您，答对了”</p>
    <p class="18">如果猜错了</p>
    <ul>
    <li><span style="font-family: 宋体">猜大了：打印</span>“<span style="font-family: Calibri">sorry</span><span style="font-family: 宋体">，您猜大了</span><span style="font-family: Calibri">!</span><span style="font-family: 宋体">”</span></li>
    <li><span style="font-family: 宋体">猜小了：打印</span>“<span style="font-family: Calibri">sorry</span><span style="font-family: 宋体">，您猜小了</span><span style="font-family: Calibri">!</span><span style="font-family: 宋体">”</span></li>
    <li class="18">直到数字猜到为止</li>
    </ul>
    <h3>思路</h3>
    <p><span style="line-height: 1.5">1.通过Random类中方法nextInt（），生成一个1-100之间的随机数</span></p>
    <p class="18">2.输入猜的数字</p>
    <p class="18">3.<span style="font-family: 宋体">通过</span>while循环，进行猜数字对错判断</p>
    <p class="18">猜对，跳出循环，游戏结束</p>
    <p class="18">猜错了，根据结果，给出提示，接着猜数字，游戏继续</p>
    <p class="18"><span style="font-family: 宋体">如果猜大了，打印</span>sorry，您猜大了!继续下一次循环</p>
    <p class="18"><span style="font-family: 宋体">如果猜小了，打印</span>sorry，您猜小了!继续下一次循环</p>
    <h3><strong>实现代码步骤</strong></h3>
    <p><span style="font-family: 宋体">分析完毕之后，</span><span style="font-family: 宋体">完成代码的编写：</span><span style="font-family: Calibri">GuessNumber.java</span></p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.Random;
    </span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.Scanner;
    
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> GuessNumber {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1.通过Random类中方法nextInt（），生成一个1-100之间的随机数</span>
            <span style="color: rgba(0, 0, 255, 1)">int</span> randomNumber = <span style="color: rgba(0, 0, 255, 1)">new</span> Random().nextInt(100<span style="color: rgba(0, 0, 0, 1)">);
            System.out.println(</span>"随机数已生成！"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2.输入猜的数字</span>
            System.out.println("----请输入您猜的数字：----"<span style="color: rgba(0, 0, 0, 1)">);
            Scanner sc </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Scanner(System.in);
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> enterNumber =<span style="color: rgba(0, 0, 0, 1)"> sc.nextInt();
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">3.通过while循环，进行猜数字对错判断
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">猜对，跳出循环，游戏结束</span>
            <span style="color: rgba(0, 0, 255, 1)">while</span> (enterNumber !=<span style="color: rgba(0, 0, 0, 1)"> randomNumber) {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">猜错了，根据结果，给出提示，接着猜数字，游戏继续</span>
                <span style="color: rgba(0, 0, 255, 1)">if</span> (enterNumber &gt;<span style="color: rgba(0, 0, 0, 1)"> randomNumber) {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果猜大了，打印sorry，您猜大了!继续下一次循环</span>
                    System.out.println("sorry，您猜大了!继续下一次循环"<span style="color: rgba(0, 0, 0, 1)">);
                } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果猜小了，打印sorry，您猜小了!继续下一次循环</span>
                    System.out.println("sorry，您猜小了!继续下一次循环"<span style="color: rgba(0, 0, 0, 1)">);
                }
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">输入猜的数字</span>
                System.out.println("----请输入您猜的数字：----"<span style="color: rgba(0, 0, 0, 1)">);
                enterNumber </span>=<span style="color: rgba(0, 0, 0, 1)"> sc.nextInt();
            }
            System.out.println(</span>"恭喜您，答对了！"<span style="color: rgba(0, 0, 0, 1)">);
        }
    
    }</span></pre>
    </div>
    <p>运行结果：</p>
    <p><img src="https://images2015.cnblogs.com/blog/612774/201707/612774-20170702124615571-1341936766.png" alt=""></p>
    <p>&nbsp;</p>
    </div>

</body>
</html>